package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.*;
import com.jiawa.train.business.domain.Req.DailyTrainCarriageQueryReq;
import com.jiawa.train.business.domain.Req.DailyTrainCarriageSaveReq;
import com.jiawa.train.business.domain.resp.DailyTrainCarriageQueryResp;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.DailyTrainCarriageMapper;
import com.jiawa.train.common.req.PageReq;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainCarriageService {

    @Autowired
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;

    @Autowired
    private TrainCarriageService trainCarriageService;

    public PageResp<DailyTrainCarriageQueryResp> queryList(@Valid DailyTrainCarriageQueryReq req) {
        DailyTrainCarriageExample example = new DailyTrainCarriageExample();
        example.setOrderByClause("date desc");
        PageHelper.startPage(req.getPage(),req.getSize());
        List<DailyTrainCarriage> carriageList = dailyTrainCarriageMapper.selectByExample(example);
        List<DailyTrainCarriageQueryResp> queryResps = BeanUtil.copyToList(carriageList, DailyTrainCarriageQueryResp.class);
        PageInfo pageInfo = new PageInfo(carriageList);

        PageResp<DailyTrainCarriageQueryResp> resp = new PageResp<>();
        resp.setList(queryResps);
        resp.setTotal(pageInfo.getTotal());
        return resp;
    }

    public void save(@Valid DailyTrainCarriageSaveReq req) {
        DailyTrainCarriage carriage = new DailyTrainCarriage();
        // 自动计算出列数和总座位数
        List<SeatColEnum> seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getColCount() * req.getRowCount());
        BeanUtils.copyProperties(req, carriage);
        carriage.setDate(req.getDate());
        if(ObjectUtil.isNull(carriage.getId())){
            carriage.setId(SnowUtil.getSnowflakeNextId());
            carriage.setCreateTime(new Date());
            carriage.setUpdateTime(new Date());
            dailyTrainCarriageMapper.insert(carriage);
        }else {
            carriage.setUpdateTime(new Date());
            dailyTrainCarriageMapper.updateByPrimaryKeySelective(carriage);
        }
    }

    public void delete(Long id) {
        dailyTrainCarriageMapper.deleteByPrimaryKey(id);
    }

    public void genDaily(Date date,String trainCode){
        //删除某日某次的车厢信息
        DailyTrainCarriageExample example = new DailyTrainCarriageExample();
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        dailyTrainCarriageMapper.deleteByExample(example);

        //查出某车次的所有车站信息
        List<TrainCarriage> CarriageList = trainCarriageService.selectByTrainCode(trainCode);
        if(CollUtil.isEmpty(CarriageList)){
            return;
        }

        for(TrainCarriage carriage : CarriageList){
            DailyTrainCarriage trainCarriage = BeanUtil.copyProperties(carriage, DailyTrainCarriage.class);
            trainCarriage.setUpdateTime(new Date());
            trainCarriage.setCreateTime(new Date());
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setDate(date);
            dailyTrainCarriageMapper.insert(trainCarriage);
        }
    }

    public List<DailyTrainCarriage> selectByTrainCode(Date date,String trainCode,String seatType){
        DailyTrainCarriageExample example = new DailyTrainCarriageExample();
        example.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)

                ;

        return dailyTrainCarriageMapper.selectByExample(example);
    }
}
